#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: Tlinux.py 
@version:
@time: 2022/03/18 
"""
#!usr/bin/env python
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: use_linux.py.py 
@version:
@time: 2021/06/29 
"""
import paramiko

# ssh = paramiko.SSHClient()
#
# ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#
# # ssh.connect("192.168.3.197", username="root", port=22, password="123456")
# ssh.connect("192.168.1.183", username="root", port=22, password="Gotapd8!!")
# # 使用这个连接执行命令
#
# agent_cmd = """docker run -itd ccr.ccs.tencentyun.com/tapdata/flow-engine:0.1 'wget "http://resource.tapdata.net/package/feagent/dfs-v1.0.1-6-28-g216e5ac-test/tapdata" && chmod +x tapdata && ./tapdata start backend --downloadUrl http://resource.tapdata.net/package/feagent/dfs-v1.0.1-6-28-g216e5ac-test/ --token a/HZzXh5MDbwPGd8hCzZYYF0XXgDZ287oY34Sx3QAq5Z7zikkMRcI62kZHXq8RRJj6VrJcSY6ehw4iM8d8LW1UN6nDTYzqVxQMoWZiHU0fw5G1n4U7zlkSF3MXJdbNST6qz9JwzvQ0xGAu7Z4HiIoCshn+5UPPvYGC6+k/rxcVCnsun01mDpCDTAXYx9UT8bQONjqWPitsqv6pKr1rtzX2D+AmoLylRK0noRJBK2cHP8zqMPxdoHH7kmIhhfHNX4BqQXRHRo1/BEpHwGuZMo5iBLCKG0FGte7UEK8vy8q2tL4ONjGam9798ODdnyXu56XITwieugt7GdDtelJhsD0JsMrmmRT5+3si3UttzvhFccyCoM69nEe8sHZC2yCAmUCGIA1Zsk+KKCRzVUx3TC+8DxKuvyrFOaZ4xxJbVPIHJxD1os2IBsNVobMDRk0WVLjMMgwAAJFBYA6Gzhj/RIMarHaCkrO5abb/SnLStUCa2mTdbuTN6XTlg/oU1BiyyH'"""
# # ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(agent_cmd)
# ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("ls -al")
#
# # 获取输出
# print(ssh_stdout.read())
# # print(ssh_stdin.write(""))
# print(ssh_stderr.read())
#
# # 关闭连接
# ssh.close()
encoding = 'utf-8'


class UseLinux():

    def __init__(self, ip="192.168.1.183", port=22, username="root", password="Gotapd8!!"):
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(hostname=ip, port=port, username=username, password=password)

    def create_agent_conatiner(self, agent_cmd):
        _in, _out, _err = self.ssh.exec_command(agent_cmd)
        _out = str(_out.read(), encoding=encoding)
        _err = str(_err.read(), encoding=encoding)
        if _err:
            return _err
        if _out:
            return _out

    def remove_agent_container(self, id):
        _in, _out, _err = self.ssh.exec_command(f"docker  rm -f {id}")
        _out = str(_out.read(), encoding=encoding)
        _err = str(_err.read(), encoding=encoding)
        if _err:
            return _err
        if _out:
            return _out

    def ssh_cmd(self, cmd):
        _in, _out, _err = self.ssh.exec_command(cmd)
        _out = str(_out.read(), encoding=encoding)
        _err = str(_err.read(), encoding=encoding)
        if _err:
            return _err
        if _out:
            return _out

    def close(self):
        self.ssh.close()

    def __del__(self):
        self.ssh.close()


if __name__ == '__main__':
    cmd = UseLinux(ip="36.134.131.197", port=22, username="root", password="Gotapd8#")
    res = cmd.ssh_cmd("docker ps")
    print(res)
